/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CSKhachHang.java
 *
 * Created on Dec 18, 2012, 1:12:35 PM
 */
package CLIENT;
import BLL.*;
import DAL.*;
import DTO.*;
import CLIENT.TToan;
import java.awt.Frame;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import java.sql.Date;
import java.text.SimpleDateFormat;
import javax.swing.JFrame;
/**
 *
 * @author Huong Giang
 */
public class CSKhachHang extends javax.swing.JFrame {
    /** Creates new form KhachHang */
    public CSKhachHang() throws Exception {
        initComponents();
        loadData();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        txtkey = new javax.swing.JTextField();
        bttSearch = new javax.swing.JButton();
        bttDel = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Khách Hàng");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "CMT", "Tên", "Phòng", "Giá", "Kiểu TT", "Ngày Thuê", "Ngày Trả"
            }
        ));
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(1).setResizable(false);

        jLabel1.setText("CMT");

        bttSearch.setText("Tìm kiếm");
        bttSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bttSearchActionPerformed(evt);
            }
        });

        bttDel.setText("Trả phòng");
        bttDel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bttDelActionPerformed(evt);
            }
        });

        jButton1.setText("Thoát");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Về menu");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Thanh toán");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(86, 86, 86)
                .addComponent(jButton3)
                .addGap(72, 72, 72)
                .addComponent(bttDel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 77, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addGap(65, 65, 65)
                .addComponent(jButton1)
                .addGap(33, 33, 33))
            .addGroup(layout.createSequentialGroup()
                .addGap(68, 68, 68)
                .addComponent(jLabel1)
                .addGap(35, 35, 35)
                .addComponent(txtkey, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 81, Short.MAX_VALUE)
                .addComponent(bttSearch)
                .addGap(256, 256, 256))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(21, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 604, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtkey, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bttSearch))
                .addGap(26, 26, 26)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton3)
                    .addComponent(bttDel)
                    .addComponent(jButton2))
                .addContainerGap(89, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
private void loadData() throws Exception
    {
         ArrayList<KhachHang> list=KhachHangBLL.selectKHall();
        DefaultTableModel dtm = new DefaultTableModel();
        dtm.addColumn("CMT");
        dtm.addColumn("Tên");
        dtm.addColumn("Mã phòng");
        dtm.addColumn("Giá");
        dtm.addColumn("Kiểu thanh toán");
        dtm.addColumn("Ngày thuê");
        dtm.addColumn("Ngày Trả");
        for (int i = 0; i < list.size(); i++) {
            KhachHang emp = list.get(i);
            Vector vec = new Vector();
            vec.add(emp.getCmt());
            vec.add(emp.getTen());
            vec.add(emp.getMaPhong());
            vec.add(emp.getGia());
            vec.add(emp.getKieuTT());
            vec.add(emp.getNgayThue());
            vec.add(emp.getNgayTra());
            dtm.addRow(vec);
        }
        this.jTable1.setModel(dtm);
         
    }
private void bttDelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bttDelActionPerformed
         int row = this.jTable1.getSelectedRow();
    if (row >= 0) {
            try {
                String cmt = this.jTable1.getValueAt(row, 0).toString();
                int ma= Integer.parseInt(this.jTable1.getValueAt(row, 2).toString());
                boolean result = KhachHangBLL.deleteKH(cmt);                 
                if (result) {
                    JOptionPane.showMessageDialog(this, "Thành công");
                       this.loadData();
                       RoomBLL.updateTrong(ma);
                       
                } else {
                    JOptionPane.showMessageDialog(this, "Lỗi");
                }
            } catch (Exception ex) {
                Logger.getLogger(CSKhachHang.class.getName()).log(Level.SEVERE, null, ex);
            }
    } else {
        JOptionPane.showMessageDialog(this, "Vui lòng chọn khách hàng");
    }   
}//GEN-LAST:event_bttDelActionPerformed
private void search(){
        int keyword= Integer.parseInt(this.txtkey.getText());
        ArrayList<KhachHang>list= KhachHangBLL.searchKHByCMT(keyword);
        DefaultTableModel dtm=new DefaultTableModel();
        dtm.addColumn("CMT");
        dtm.addColumn("Tên");
        dtm.addColumn("Mã phòng");
        dtm.addColumn("Giá");
        dtm.addColumn("Kiểu thanh toán");
        dtm.addColumn("Ngày thuê");
        dtm.addColumn("Ngày Trả");
        for (int i = 0; i < list.size(); i++) {
            KhachHang emp = list.get(i);
            Vector vec = new Vector();
            vec.add(emp.getCmt());
            vec.add(emp.getTen());
            vec.add(emp.getMaPhong());
            vec.add(emp.getGia());
            vec.add(emp.getKieuTT());
            vec.add(emp.getNgayThue());
            vec.add(emp.getNgayTra());
            dtm.addRow(vec);
        }
        this.jTable1.setModel(dtm);
    }
   
private void bttSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bttSearchActionPerformed
      this.search();
}//GEN-LAST:event_bttSearchActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
    Main m =new Main();
    m.show();
    this.dispose();
}//GEN-LAST:event_jButton2ActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
    this.dispose();
}//GEN-LAST:event_jButton1ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
     int row = this.jTable1.getSelectedRow();
    if (row >= 0) {
        String cm = this.jTable1.getValueAt(row, 0).toString();
        String m =this.jTable1.getValueAt(row, 2).toString();
        String donvi =this.jTable1.getValueAt(row, 4).toString();
        String gia= this.jTable1.getValueAt(row, 3).toString();
        int tien = Integer.parseInt(gia);
        TToan tt=new TToan(this);
        this.dispose();
        tt.show();
        tt.setVisible(true);
        tt.setCMT(cm);
        tt.setMaphong(m);
        tt.setKieu(donvi);
        tt.setGia(tien);
         } 
    else {
        JOptionPane.showMessageDialog(this, "Vui lòng chọn khách hàng");
    }
}//GEN-LAST:event_jButton3ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CSKhachHang.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CSKhachHang.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CSKhachHang.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CSKhachHang.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    new CSKhachHang().setVisible(true);
                } catch (Exception ex) {
                    Logger.getLogger(CSKhachHang.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bttDel;
    private javax.swing.JButton bttSearch;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField txtkey;
    // End of variables declaration//GEN-END:variables
}
